package pers.vic.util.codec;

import org.apache.commons.codec.binary.Base64;

/**
 * 对commons下Base64的扩展
 * @author VIC
 *
 */
public class Base64Extends extends Base64 {

	/**
	 * 带盐值的base64加密
	 * @param srcData
	 * @param salt
	 * @return
	 */
	public static String encodeDataInfo(String srcData, String salt) {
		byte[] xorArray = salt.getBytes();
		byte[] srcInfo = srcData.getBytes();
		for (int i = 0; i < srcInfo.length; i++) {
			for (int j = 0; j < xorArray.length; j++) {
				srcInfo[i] = (byte) (srcInfo[i] ^ xorArray[j]);
			}
		}
		byte[] baseStr = encodeBase64(srcInfo);
		return new String(baseStr);
	}
	/**
	 * 带盐值的base64解密
	 * @param dicData
	 * @param salt
	 * @return
	 */
	public static String decodeDataInfo(String dicData, String salt) {
		byte[] xorArray = salt.getBytes();
	    byte[] dicInfo = decodeBase64(dicData.getBytes());
	    for (int i = 0; i < dicInfo.length; i++) {
	      for (int j = 0; j < xorArray.length; j++) {
	        dicInfo[i] = (byte)(dicInfo[i] ^ xorArray[j]);
	      }
	    }
	    String srcData = new String(dicInfo);
	    return srcData;
	  }
	
	
	/*
	 * for test
	 */
	public static void main(String[] args) {
		String srcData = "this is data.";
		String salt = "vic123";
		String dicData = encodeDataInfo(srcData, salt);
		String data = decodeDataInfo(dicData, salt);
		System.out.println(dicData);
		System.out.println(data);
	}
}
